package com.freshfresh.activity.classification;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.freshfresh.activity.BaseActivity;
import com.freshfresh.activity.R;
import com.freshfresh.adapter.GuigeAdapter;
import com.freshfresh.utils.ImageLoadOptions;
import com.freshfresh.utils.UrlConstants;
import com.freshfresh.utils.Utils;
import com.freshfresh.view.AlertDialog;
import com.freshfresh.view.autoscroll.Imageloader_homePager;
import com.freshfresh.view.autoscroll.TagViewPager;
import com.freshfresh.view.autoscroll.TagViewPager.OnGetView;
import com.nostra13.universalimageloader.core.ImageLoader;

//商品详情页
//测试url=http://www.freshfresh.com/catalog/product/getProductInfo?productid=2341&sku=
//productid sku  categoryid
public class ProductDetailsActivity extends BaseActivity implements Serializable{
	TagViewPager myviewpager;//banner
	ImageView iv_nationalFlag;//国旗
	TextView tv_proName;//商品名称
	TextView tv_proSaleStatus;//库存状态
	TextView tv_proDescribe;//描述
	TextView tv_marketPrice;//超市参考价
	TextView tv_addressFrom;//产地
	TextView tv_consultPrice;//danwei价格
	TextView tv_proDetialDesc;//商品描述
	TextView tv_vegetables_product_price;//价格
	ImageView iv_ac_user_center_calling2;//拨打电话
	TextView tv_product_details_pending_type;//配送方式
	TextView tv_title;//标题
	RelativeLayout rel_back;//返回
	private RelativeLayout rl_graphic_details=null;//图文
	private RelativeLayout rl_product_detials_comment=null;//评论列表
	
	Map<String,Object> mapdata;
	Map<String,Object> map_guige;
	List<Map<String,Object>> listmap_guige;
	FrameLayout query_frame;
	Dialog progressDialog;
	private List<String> listmap_banner;//banner的数据
	
	private TextView tv_standard1=null;
	//判断多规格布局
	private LinearLayout rg_dialog_xuanze=null;//仅一种
	private GridView gvs_type=null;//多规格
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_product_details);
		initPDialog();
		initView();
		getData();
	}

	public void initView() {
		tv_standard1 = (TextView) findViewById(R.id.tv_standard1);
		rg_dialog_xuanze = (LinearLayout) findViewById(R.id.rg_dialog_xuanze);
		gvs_type = (GridView) findViewById(R.id.gvs_type);
		myviewpager = (TagViewPager) findViewById(R.id.myviewpager);
		query_frame = (FrameLayout) findViewById(R.id.query_frame);
		iv_nationalFlag = (ImageView) findViewById(R.id.iv_nationalFlag);
		rl_product_detials_comment = (RelativeLayout) findViewById(R.id.rl_product_detials_comment);
		rl_graphic_details = (RelativeLayout) findViewById(R.id.rl_graphic_details);
		tv_proName = (TextView) findViewById(R.id.tv_proName);
		tv_vegetables_product_price = (TextView) findViewById(R.id.tv_vegetables_product_price);
		tv_proDetialDesc = (TextView) findViewById(R.id.tv_proDetialDesc);
		tv_product_details_pending_type = (TextView) findViewById(R.id.tv_product_details_pending_type);
		iv_ac_user_center_calling2 = (ImageView) findViewById(R.id.iv_ac_user_center_calling2);
		tv_title = (TextView) findViewById(R.id.tv_title);
		rel_back = (RelativeLayout) findViewById(R.id.rel_back);
		tv_title.setText("商品详情");
		tv_consultPrice = (TextView) findViewById(R.id.tv_consultPrice);
		tv_marketPrice = (TextView) findViewById(R.id.tv_marketPrice);
		tv_proDescribe = (TextView) findViewById(R.id.tv_proDescribe);
		tv_proSaleStatus = (TextView) findViewById(R.id.tv_proSaleStatus);
		tv_addressFrom = (TextView) findViewById(R.id.tv_addressFrom);
		rel_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				ProductDetailsActivity.this.finish();
			}
		});
		iv_ac_user_center_calling2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				 new AlertDialog(ProductDetailsActivity.this).builder().setMsg("是否拨打两鲜热线:" + getResources().getString(R.string.liangxian_tel) + "?").setNegativeButton("", null).setPositiveButton("拨打", new View.OnClickListener()
			        {
			          public void onClick(View paramAnonymousView)
			          {
			        	  Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
									+ getResources().getString(R.string.liangxian_tel)));
							startActivity(intent);
			          }
			        }).show();
			}
		});
		
		rl_graphic_details.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent=new Intent(ProductDetailsActivity.this,ProductImageActivity.class);
				intent.putExtra("list", (Serializable)listmap_banner);
				startActivity(intent);
			}
		});
		
		rl_product_detials_comment.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent=new Intent(ProductDetailsActivity.this,ProductCommentActivity.class);
				intent.putExtra("list", (Serializable)listmap_banner);
				intent.putExtra("national_flag", mapdata.get("national_flag").toString());
				intent.putExtra("name", mapdata.get("name").toString());
				startActivity(intent);
			}
		});
	}

	//加载进度
	public void initPDialog(){
		progressDialog=Utils.initProgressDialog(ProductDetailsActivity.this);
	}
	//获取详情页数据
	public void getData(){
		progressDialog.show();
		//productid sku  categoryid
		Map<String, String> params = new HashMap<String, String>();
		params.put("productid", getIntent().getExtras().getString("productid"));
		params.put("sku", getIntent().getExtras().getString("sku"));
		String ad_url = UrlConstants.product_detail_url;
		Log.e("url是多少…………", ad_url);
		Log.e("productid是多少…………", getIntent().getExtras().getString("productid"));
		Log.e("sku是多少…………", getIntent().getExtras().getString("sku"));
		executeRequest(new StringRequest(ad_url, params,
				new Response.Listener<String>() {
					public void onResponse(String arg0) {
						progressDialog.dismiss();
						Log.e("listmap的……", arg0);
						if (Utils.parseJsonStr(arg0).get("result").toString().equals("1")) {
						    mapdata=(Map<String,Object>)Utils.parseJsonStr(arg0).get("data");
							listmap_banner=(List<String>)mapdata.get("images");
							
							initBanner();
							//加载国旗图标
							ImageLoader.getInstance().displayImage(mapdata.get("national_flag").toString(),iv_nationalFlag, ImageLoadOptions.getOptions(R.drawable.guoqi));
							if(mapdata.get("name")!=null){
							tv_proName.setText(mapdata.get("name").toString());	
							}
							
//							tv_marketPrice
							if(mapdata.get("feature_description")!=null){
								tv_proDescribe.setText(mapdata.get("feature_description").toString());	
							}
							
							
							
							if(mapdata.get("origin_text")!=null){
								if(mapdata.get("origin_text").toString().equals("")){
									tv_addressFrom.setText("产地：暂无");	
								}else{
									tv_addressFrom.setText(mapdata.get("origin_text").toString());	
								}
							}else{
								tv_addressFrom.setText("产地：暂无");
							}
							//单位价格tv_consultPrice  booking_price
							if(mapdata.get("booking_price")!=null){
								if(mapdata.get("booking_price").toString().equals("")){
									tv_consultPrice.setText("单位价格：暂无");	
								}else{
									tv_consultPrice.setText("单位价格："+mapdata.get("booking_price").toString());	
								}
							}else{
								tv_consultPrice.setText("单位价格：暂无");
							}
							//商品描述 tv_proDetialDesc
							if(mapdata.get("short_description")!=null){
								tv_proDetialDesc.setText(mapdata.get("short_description").toString());	
							}
							//价格tv_vegetables_product_price
							if(mapdata.get("type")!=null){
								if(mapdata.get("type").toString().equals("simple")){
									rg_dialog_xuanze.setVisibility(View.VISIBLE);
									gvs_type.setVisibility(View.GONE);
									map_guige=(Map<String,Object>)mapdata.get("product_info"); 
									
									tv_vegetables_product_price.setText(map_guige.get("special_price").toString());
									if(map_guige.get("standard2")!=null){
										tv_standard1.setText(map_guige.get("standard2").toString());
									}
									
									if(map_guige.get("is_in_stock")!=null){
										if(map_guige.get("is_in_stock").toString().equals("0")){
											tv_proSaleStatus.setText("缺货");
											tv_proSaleStatus.setBackgroundColor(R.drawable.bg_gray_deep_corner);
										}else if(map_guige.get("is_in_stock").toString().equals("1")){
											tv_proSaleStatus.setText("现货");
											tv_proSaleStatus.setBackgroundResource(R.drawable.bg_green_deep_corner);
										}
									}
									
									if(map_guige.get("supermarket_price")!=null){
										if(map_guige.get("supermarket_price").toString().equals("")){
											tv_marketPrice.setText("暂无");	
										}else{
											tv_marketPrice.setText("超市参考价："+map_guige.get("supermarket_price").toString());	
										}
									}else{
										tv_marketPrice.setText("暂无");
									}
									
								}else if(mapdata.get("type").toString().equals("bundle")){
									rg_dialog_xuanze.setVisibility(View.GONE);
									gvs_type.setVisibility(View.VISIBLE);
									listmap_guige=(List<Map<String,Object>>)mapdata.get("product_info"); 
									
									
									final GuigeAdapter adapters=new GuigeAdapter(context, listmap_guige);
						        	gvs_type.setAdapter(adapters);
						        	if(listmap_guige!=null&&listmap_guige.size()!=0){
						        		
				        			tv_vegetables_product_price.setText(listmap_guige.get(0).get("special_price").toString());
				        			
				        			if(listmap_guige.get(0).get("is_in_stock")!=null){
										if(listmap_guige.get(0).get("is_in_stock").toString().equals("0")){
											tv_proSaleStatus.setText("缺货");
											tv_proSaleStatus.setBackgroundColor(R.drawable.bg_gray_deep_corner);
										}else if(listmap_guige.get(0).get("is_in_stock").toString().equals("1")){
											tv_proSaleStatus.setText("现货");
											tv_proSaleStatus.setBackgroundResource(R.drawable.bg_green_deep_corner);
										}
									}
									
									if(listmap_guige.get(0).get("supermarket_price")!=null){
										if(listmap_guige.get(0).get("supermarket_price").toString().equals("")){
											tv_marketPrice.setText("暂无");	
										}else{
											tv_marketPrice.setText("超市参考价："+listmap_guige.get(0).get("supermarket_price").toString());	
										}
									}else{
										tv_marketPrice.setText("暂无");
									}
						        	}
						        	gvs_type.setOnItemClickListener(new OnItemClickListener() {

										@Override
										public void onItemClick(AdapterView<?> arg0, View arg1,
												int arg2, long arg3) {
											
											if(listmap_guige.get(arg2).get("is_in_stock")!=null){
												if(listmap_guige.get(arg2).get("is_in_stock").toString().equals("0")){
													tv_proSaleStatus.setText("缺货");
													tv_proSaleStatus.setBackgroundColor(R.drawable.bg_gray_deep_corner);
												}else if(listmap_guige.get(arg2).get("is_in_stock").toString().equals("1")){
													tv_proSaleStatus.setText("现货");
													tv_proSaleStatus.setBackgroundResource(R.drawable.bg_green_deep_corner);
												}
											}
											
											if(listmap_guige.get(arg2).get("supermarket_price")!=null){
												if(listmap_guige.get(arg2).get("supermarket_price").toString().equals("")){
													tv_marketPrice.setText("暂无");	
												}else{
													tv_marketPrice.setText("超市参考价："+listmap_guige.get(arg2).get("supermarket_price").toString());	
												}
											}else{
												tv_marketPrice.setText("暂无");
											}
											
											
											if(listmap_guige!=null&&listmap_guige.size()!=0){
												tv_vegetables_product_price.setText(listmap_guige.get(arg2).get("special_price").toString());
									    		adapters.setSeclection(arg2);
									    		adapters.notifyDataSetChanged();
								        	}
										}
									});
									
									
								}
							}
							
							
							
						} else {
							
						}
					}

				}, new Response.ErrorListener() {

					public void onErrorResponse(VolleyError arg0) {
						// dialog.dismiss();
						Toast.makeText(ProductDetailsActivity.this, "获取失败", 1).show();
						progressDialog.dismiss();
					}
				}));
	}
	
	public void initBanner(){
		//
		Log.e("listmap_banner长度是多少…………", listmap_banner.size()+"");
		myviewpager.init(R.drawable.indicator_focused,
				R.drawable.indicator, 10, 5, 2, 20);
		myviewpager.setAutoNext(true, 2000);
		myviewpager.setOnGetView(new OnGetView() {

			@Override
			public View getView(ViewGroup container, int position) {
				// TODO Auto-generated method stub
				ImageView iv = new ImageView(ProductDetailsActivity.this);
				iv.setScaleType(ScaleType.FIT_XY);
				
				ImageLoader.getInstance().displayImage(
						listmap_banner.get(position), iv,
						ImageLoadOptions.getOptions(0));
				container.addView(iv);
				return iv;
			}
		});
		myviewpager.setAdapter(listmap_banner.size());
		// // 强制控件高度
		LinearLayout.LayoutParams titl = (LinearLayout.LayoutParams) query_frame.getLayoutParams(); // 取控件当前的布局参数
		titl.height = Utils.getScreenHeight(ProductDetailsActivity.this)  / 3;// 控件的高强制设成
		query_frame.setLayoutParams(titl); // 使设置好的布局参数应用到控件
		
		FrameLayout.LayoutParams tit2 = (FrameLayout.LayoutParams) myviewpager.getLayoutParams(); // 取控件当前的布局参数
		tit2.height = Utils.getScreenHeight(ProductDetailsActivity.this)  / 3;// 控件的高强制设成
		myviewpager.setLayoutParams(tit2); // 使设置好的布局参数应用到控件
	}

}